package mju.cxf.controller.user;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import mju.cxf.constant.MessageConstant;
import mju.cxf.context.BaseContext;
import mju.cxf.dto.CollectAddDTO;
import mju.cxf.entity.Collect;
import mju.cxf.result.Result;
import mju.cxf.service.CollectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * <p>
 * 收藏表 前端控制器
 * </p>
 *
 * @author 乐昌新
 * @since 2024-10-28
 */
@RestController
@RequestMapping("/user/api/collection")
public class ICollectionController {
    @Autowired
    private CollectionService collectionService;

    @PostMapping
    public Result<String> statusCollection (@RequestBody CollectAddDTO collectAddDTO) {
        if (collectAddDTO.isType()) {
            LambdaQueryWrapper<Collect> lqw = new LambdaQueryWrapper<>();
            lqw.eq(Collect::getProductId, collectAddDTO.getSpecificationsId()).eq(Collect::getUserId, BaseContext.getCurrentId());
            return collectionService.remove(lqw) ?
                    Result.success(MessageConstant.SUCCESS_MESSAGE) :
                    Result.error(MessageConstant.FAILURE_MESSAGE);
        } else {
            Collect collect = new Collect();
            collect.setUserId(BaseContext.getCurrentId());
            collect.setProductId(collectAddDTO.getSpecificationsId());
            collect.setCreateTime(LocalDateTime.now());
            return collectionService.save(collect) ?
                    Result.success(MessageConstant.SUCCESS_MESSAGE) :
                    Result.error(MessageConstant.FAILURE_MESSAGE);
        }

    }
}
